package com.hsbc.dev.teamo4.sms.sm.dao.impl;

import com.google.common.collect.Lists;
import com.hsbc.dev.teamo4.sms.common.utils.data.DBUtil;
import com.hsbc.dev.teamo4.sms.common.utils.factory.DBConnectFactory;
import com.hsbc.dev.teamo4.sms.sm.dao.IRoleDao;
import com.hsbc.dev.teamo4.sms.sm.po.Permission;
import com.hsbc.dev.teamo4.sms.sm.po.Role;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.sql.SQLException;
import java.util.List;

/**
 * @author:yang
 * @version:
 * @Project: permission_manage
 * @Package: com.hsbc.dev.teamo4.sms.sm.dao.impl
 * @Description:
 * @Date date: 2018/7/27
 */
public class RoleDaoImpl implements IRoleDao {
    @Override
    public Role saveEntity(Role role) {
        String sql="insert into sm_role values(?,?,?,?,?,?,?,?)";
        return (Role) DBUtil.saveEntity(sql,role,null);
    }

    @Override
    public Role updateEntity(Role role) {
        String sql="update sm_role set roleName=?,roleGrade=?,createId=?,createTime=?,updateId=?,updateTime=?,isEnable=? where id=?";
        return (Role) DBUtil.updateEntity(sql,role, Lists.newArrayList("id","createId","createTime"),role.getId());
    }

    @Override
    public int updateByIsEnable(String id, Boolean bool) {
        String sql = "update sm_role set isEnable=? where id=?";
        return DBUtil.updateByIsEnable(sql,id,bool);
    }

    @Override
    public Role queryById(String id) {
        String sql="select * from sm_role where id='"+id+"'";
        BeanHandler beanHandler=new BeanHandler<>(Role.class);
        return (Role) DBUtil.queryById(sql,beanHandler);
    }

    @Override
    public List<Role> queryAllEntity() {
        String sql="select * from sm_role where isEnable='"+true+"'";
        BeanListHandler beanHandler=new BeanListHandler<>(Role.class);
        BeanListHandler beanListHandler=new BeanListHandler<>(Permission.class);
        return (List<Role>) DBUtil.queryAllEntity(sql,beanListHandler);
    }

    @Override
    public List<Role> queryLike(String name) {
//        String sql="select * from sm_role where roleName like %'"+name+"'% or roleGrade like %'"+name+"'% or id like %'"+name+"'%";
       String sql="select * from sm_role where roleName like '%"+name+"%'or roleGrade like '%"+name+"%'or id like'%"+name+"%'";
        BeanListHandler beanListHandler=new BeanListHandler<>(Role.class);
       return (List<Role>) DBUtil.queryAllEntity(sql,beanListHandler);
    }
}
